<?php
/**
 *
 *
 */
namespace Yz\modules\Uploads;
use Yz_Array,
    Yz_Hash,
    Error
    ;
class Controller_Upload
    extends \Yz_Controller_Templated
{

    /** prevent the controller from appearing in `actions menu`
     *  @const false
     */
    const IN_MENU = false;

    protected $layoutName = false;
    protected $modelName = 'Upload';

    /****************************
     *      Methods
     ****************************/

    /**
     *
     */
    public function doGET(Yz_Array $args)
    {
        debug();
        $template = $this->selectTemplate("form");

        $this->outputTemplate($template);
        return $this;
    }

    /**
     *
     */
    public function doError(Error $ex)
    {
        debug($ex->getTraceAsString());
        $template = $this->selectTemplate('uploadFailed');
        $template->error = $ex;
        $this->getView()->setCacheable(false);
        $this->outputTemplate($template);
        return $this;
    }


    /**
     *
     */
    public function doPOST(Yz_Array $args, Yz_Hash $post)
    {
        $upload = $this->module->selectModel($this->modelName);
        $item = $upload->processUpload('theUpload');
        $item->save();

        $template = $this->selectTemplate('uploadComplete');
        $template->upload = $item;

        $this->getView()->setCacheable(false);
        $this->outputTemplate($template);
        return $this;
    }

}
